package com.example.demo.Controller;

import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;

@Service
public class Logger {
    int count = 0;
    File file;
    FileOutputStream fileOutputStream;

    public Logger() throws IOException {
        file = new File("log");
        file.mkdir();
    }

    public void log(String msg) throws IOException {
        msg = msg + System.lineSeparator();
        String fileName;

        if (count == 0 || count == 3) {
            count = 0;
            fileName = LocalDateTime.now().toString().replace(":", "");
            file = new File("log/" + fileName.substring(0, fileName.indexOf(".")) + ".log");
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(msg.getBytes());
        } else {
            fileOutputStream.write(msg.getBytes());
        }
        count++;
    }

}
